package com.mall.service.comment;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.comment.MallCommentService;
import com.mall.custom.dao.read.MallCommentReadMapper;
import com.mall.custom.dao.write.MallCommentWriteMapper;
import com.mall.pojo.MallComment;
import com.mall.pojo.MallCommentExample;
import com.mall.pojo.MallCommentExample.Criteria;
import com.mall.query.Page;
@Service
public class MallCommentServiceImpl implements MallCommentService {

	
	@Autowired
	private MallCommentReadMapper mallCommentReadMapper;
	@Autowired
	private MallCommentWriteMapper mallCommentWriteMapper;

	@Override
	public List<MallComment> getMallCommentListByMallId(Long mallId, Integer status, Integer type, Page page) {
		MallCommentExample mallCommentExample = new MallCommentExample();
		mallCommentExample.setStart(page.getBegin());
		mallCommentExample.setLimit(page.getLength());
		mallCommentExample.setOrderByClause(" id desc");
		Criteria criteria = mallCommentExample.createCriteria();
		criteria.andMallIdEqualTo(mallId);
		if (status != null) {
			criteria.andStatusEqualTo(status);
		}
		if (type != null) {
			criteria.andTypeEqualTo(type);
		}
		return this.mallCommentReadMapper.selectByExample(mallCommentExample);
	}

	@Override
	public int countMallCommentListByMallId(Long mallId, Integer status, Integer type) {
		MallCommentExample mallCommentExample = new MallCommentExample();
		Criteria criteria = mallCommentExample.createCriteria();
		criteria.andMallIdEqualTo(mallId);
		if (status != null) {
			criteria.andStatusEqualTo(status);
		}
		if (type != null) {
			criteria.andTypeEqualTo(type);
		}
		return this.mallCommentReadMapper.countByExample(mallCommentExample);
	}

	@Override
	public List<MallComment> getMallCommentListByUserId(Long userId, Integer status, Integer type, Page page) {
		MallCommentExample mallCommentExample = new MallCommentExample();
		mallCommentExample.setStart(page.getBegin());
		mallCommentExample.setLimit(page.getLength());
		mallCommentExample.setOrderByClause(" id desc");
		Criteria criteria = mallCommentExample.createCriteria();
		criteria.andUserIdEqualTo(userId);
		if (status != null) {
			criteria.andStatusEqualTo(status);
		}
		if (type != null) {
			criteria.andTypeEqualTo(type);
		}
		return this.mallCommentReadMapper.selectByExample(mallCommentExample);
	}

	@Override
	public int countMallCommentListByUserId(Long userId, Integer status, Integer type) {
		MallCommentExample mallCommentExample = new MallCommentExample();
		Criteria criteria = mallCommentExample.createCriteria();
		criteria.andUserIdEqualTo(userId);
		if (status != null) {
			criteria.andStatusEqualTo(status);
		}
		if (type != null) {
			criteria.andTypeEqualTo(type);
		}
		return this.mallCommentReadMapper.countByExample(mallCommentExample);
	}

	@Override
	public MallComment getMallCommentById(Long mallCommentId) {
		return this.mallCommentReadMapper.selectByPrimaryKey(mallCommentId);
	}

	@Override
	public void insertMallComment(MallComment mallComment) {
		mallComment.setGmtCreate(new Date());
		mallComment.setGmtModify(new Date());
		this.mallCommentWriteMapper.insertSelective(mallComment);
	}

}
